using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DiseaseScrollRECT : MonoBehaviour,IBeginDragHandler,IEndDragHandler
{
    private GridLayoutGroup _grid;//网格布局
    
    //拖动名
    private ScrollRect _dscroll;
    private bool _openfirst = true;
    private int _page = 1;
    
    private GameObject _finDiseaseGrid;
    public  float DisaeaseGridchildCount;//FINHerbGrid的子物体
    public float[] DiseapagePosition;//子物体数组间隔
   
    
    // Start is called before the first frame update
    void Start()
    {
        _dscroll = GetComponent<ScrollRect>();
        _finDiseaseGrid = GameObject.FindWithTag("DiseaseGrid");
        _page = 1;
    }

    // Update is called once per frame
    void Update()
    {
        HerbContorl.Instance.PageNumber = _page;
         SetGridWidth();
         if (_openfirst)
         { 
             _dscroll.horizontalNormalizedPosition = DiseapagePosition[0];
             _openfirst = false;
         }
    }
    
    //自适应宽度
    public void SetGridWidth()
    {
        _grid = _finDiseaseGrid.gameObject.GetComponent<GridLayoutGroup>();
        DisaeaseGridchildCount = _finDiseaseGrid.gameObject.transform.childCount;//获取子物体个数
        float width = (Mathf.Ceil(DisaeaseGridchildCount / 2)) * 700;
        _grid.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,width);
        
        //获取子物体数组间隔
        float pageX = 1 / (Mathf.Ceil(DisaeaseGridchildCount / 2) - 1);
        DiseapagePosition = new float[(int)Mathf.Ceil(DisaeaseGridchildCount / 2)];
        for (int i = 0; i < (int)Mathf.Ceil(DisaeaseGridchildCount / 2); i++)
        {
            if (i == 0)
            {
                DiseapagePosition[i] = 0;
            }else if (i == (int)Mathf.Ceil(DisaeaseGridchildCount / 2)-1)
            {
                DiseapagePosition[i] = 1;
            }
            else
            {
                DiseapagePosition[i] = DiseapagePosition[i-1] + pageX;
            }
        }
    }
    
    //点击事件
    public void OnBeginDrag(PointerEventData eventData)
    {
        
        Debug.Log("开始拖拽");
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        float currentPosition = _dscroll.horizontalNormalizedPosition;//当前位置
        int index = 0;
        float offset = currentPosition - DiseapagePosition[0];//偏移量
        
        for (int i = 1; i < DiseapagePosition.Length; i++)
        {
            if (Mathf.Abs(currentPosition - DiseapagePosition[i]) < offset)
            {
                index = i;
                offset = Mathf.Abs(currentPosition - DiseapagePosition[i]);
                if (HerbContorl.Instance.PageNumber < DiseapagePosition.Length)
                {
                    _page++;
                }
            } else
            {
                if (HerbContorl.Instance.PageNumber >1)
                {
                    _page--;
                }
            }
        }
        _dscroll.horizontalNormalizedPosition = DiseapagePosition[index];
     }
    
    
}
